<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="tree" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="hAdd('')"
                    >添加一级部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" :default-expand-all="true">
          <template slot-scope="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEdit(data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="isEdit ? '编辑部门' : '添加部门'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
      >
        <addor-edit
          v-if="showDialog"
          :pid="Id"
          :isedit="isEdit"
          :origin-list="originList"
          @success="hsuccess"
          @closed="closed"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { arrTotree } from '../../utils/index'
import AddorEdit from './deptDialog.vue'
export default {
  components: { AddorEdit },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false,
      Id: '',
      isEdit: false, // 记录当前是否处于编辑,
      originList: []

    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    closed() {
      this.showDialog = false
    },
    async loadDepartments() {
      const {
        data: { depts: res }
      } = await getDepartments()
      // console.log(res)
      // 删除第一项
      res.shift()
      this.originList = res.map(({ id, pid, code, name }) => { return { id, code, pid, name } })
      this.list = arrTotree(res)
    },
    // 用户点击添加
    hAdd(id) {
      // 显示弹框
      this.showDialog = true
      // 拿到当前id传给子组件
      // console.log(id)
      this.Id = id
      this.isEdit = false
    },
    // 用户点击编辑
    hEdit(id) {
      // 更新当前id
      this.Id = id
      // 设置状态为编辑
      this.isEdit = true
      // 打开弹框
      this.showDialog = true
    },
    async  doDel(id) {
      const res = await delDepartment(id)
      console.log(res)
      // this.$message.success(res.message)
      this.loadDepartments()
    },
    // 用户点击删除
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // console.log(id)
        this.doDel(id)
      }).catch(err => err)
    },
    hsuccess() {
      this.showDialog = false
      this.loadDepartments()
    }
  }
}
</script>
